package net.learn.piggy.registrar;

import net.learn.piggy.annotation.MyImportEnable;
import net.learn.piggy.domain.MyImportBean;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import java.util.Map;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map<String, Object> attr = importingClassMetadata.getAnnotationAttributes(MyImportEnable.class.getName());
        if(attr != null){
            if((Boolean) attr.get("value")){
                GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClass(MyImportBean.class);
                registry.registerBeanDefinition("myImportBean", beanDefinition);
            }
        }
    }
}
